/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.site.manager.node.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jzwl.common.constant.Constants;
import com.jzwl.common.id.Sequence;
import com.jzwl.system.base.dao.BaseDAO;

@Repository("siteNodeDao")
public class SiteNodeDao {

	@Autowired
	private Constants constants;

	@Autowired
	private BaseDAO baseDAO;//dao基类，操作数据库

	public boolean addSiteNode(Map<String, Object> map) {

		//自动注入时间戳为ID 酌情修改数据库类型为bigint  int会越界
		map.put("id", Sequence.nextId());
		map.put("createTime", new Date());
		map.put("isDelete", 0);
		map.put("isTop", 0);
		map.put("isDisplay", 0);
		if (null == map.get("ord") || StringUtils.isEmpty(map.get("ord").toString())) {
			map.put("ord", 0);
		}
		/*if(null == map.get("ord") || StringUtils.isEmpty(map.get("ord").toString())){
			map.put("ord",  0);
		}
		if(null == map.get("ord") || StringUtils.isEmpty(map.get("ord").toString())){
			map.put("ord",  0);
		}*/
		if (null == map.get("isRedirect") || StringUtils.isEmpty(map.get("isRedirect").toString())) {
			map.put("isRedirect", 0);
		}
		if (null == map.get("redirectUrl") || StringUtils.isEmpty(map.get("redirectUrl").toString())) {
			map.put("redirectUrl", "");
		}

		String sql = "insert into "
				+ constants.getCurrentDB()
				+ "site_node "
				+ " (id,parentId,name,typeId,isTop,ord,createTime,isDelete,isRedirect,redirectUrl,imageUrl,isDisplay) "
				+ " values "
				+ " (:id,:parentId,:name,:typeId,:isTop,:ord,:createTime,:isDelete,:isRedirect,:redirectUrl,:imageUrl,:isDisplay)";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public String getColumns() {
		return "" + " t.id as id," + " t.parentId as pId," + " t.name as name," + " t.typeId as typeId,"
				+ " t.isTop as isTop," + " t.ord as ord," + " t.isRedirect as isRedirect,"
				+ " t.redirectUrl as redirectUrl," + " t.imageUrl as imageUrl," + " t.isDisplay as isDisplay,"
				+ " t.createTime as createTime ";
	}

	public List<Map<String, Object>> querySiteNodeInfo(Map<String, Object> map) {

		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_node t ";
		sql = sql + " where t.isDelete=0 and t.parentId = " + map.get("id");
		sql = sql + " order by t.ord desc,id ";

		return baseDAO.queryForList(sql);
	}

	public boolean updateSiteNode(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_node set ";
		if (null != map.get("name") && StringUtils.isNotEmpty(map.get("name").toString())) {
			sql = sql + "			name=:name ,";
		}

		if (null != map.get("typeId") && StringUtils.isNotEmpty(map.get("typeId").toString())) {
			sql = sql + "			typeId=:typeId ,";
		}

		if (null != map.get("isTop") && StringUtils.isNotEmpty(map.get("isTop").toString())) {
			sql = sql + "			isTop=:isTop ,";
		}

		if (null != map.get("ord") && StringUtils.isNotEmpty(map.get("ord").toString())) {
			sql = sql + "			ord=:ord ,";
		}
		if (null != map.get("isRedirect") && StringUtils.isNotEmpty(map.get("isRedirect").toString())) {
			sql = sql + "			isRedirect=:isRedirect ,";
		}
		if (null != map.get("redirectUrl") && StringUtils.isNotEmpty(map.get("redirectUrl").toString())) {
			sql = sql + "			redirectUrl=:redirectUrl ,";
		}
		if (null != map.get("imageUrl") && StringUtils.isNotEmpty(map.get("imageUrl").toString())) {
			sql = sql + "			imageUrl=:imageUrl ,";
		}
		if (null != map.get("isDisplay") && StringUtils.isNotEmpty(map.get("isDisplay").toString())) {
			sql = sql + "			isDisplay=:isDisplay ,";
		}

		sql = sql.substring(0, sql.length() - 1);
		sql = sql + " where id=:id";
		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean deleteSiteNode(Map<String, Object> map) {

		String sql = "delete from " + constants.getCurrentDB() + "site_node where id in (" + map.get("id") + " ) ";

		return baseDAO.executeNamedCommand(sql, map);
	}

	public boolean delSiteNode(Map<String, Object> map) {

		String sql = "update " + constants.getCurrentDB() + "site_node set " + " isDelete = 1" + " where id=:id";
		String ArtleSql = "update " + constants.getCurrentDB() + "site_article set " + " isDelete = 1"
				+ " where nodeId=:id";
		baseDAO.executeNamedCommand(ArtleSql, map);
		return baseDAO.executeNamedCommand(sql, map);
	}

	@SuppressWarnings("unchecked")
	public Map getById(Map<String, Object> map) {
		String sql = "select " + getColumns() + " from " + constants.getCurrentDB() + "site_node t where t.id = "
				+ map.get("id") + "";
		return baseDAO.queryForMap(sql);
	}

}
